<?php
class payment{
	private function pkcs5_pad ($text, $blocksize) 
	{ 
	    $pad = $blocksize - (strlen($text) % $blocksize); 
	    return $text . str_repeat(chr($pad), $pad); 
	} 
	private function pkcs5_unpad($text) 
	{ 
	    $pad = ord($text{strlen($text)-1}); 
	    if ($pad > strlen($text)) return false; 
	    if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false; 
	    return substr($text, 0, -1 * $pad); 
	}
	function encrypt($data) {
		$childKeyLocation = CHILD_KEY_LOCATION;
		$parentKeyLocation = PARENT_KEY_LOCATION;

		if(!file_exists($childKeyLocation)){
			modules::run('adminlog/saveLog',"Encrypt error","Error in Store::encrypt()-->childKey doesn't exist'<--");
		}		
		if(!file_exists($parentKeyLocation)){
			modules::run('adminlog/saveLog',"Encrypt error","Error in Store::encrypt()-->parentKey doesn't exist'<--");
		}

		$algorithm = MCRYPT_RIJNDAEL_128;
		$blockMode = MCRYPT_MODE_ECB;
		// Load parent key
		$handle = fopen($parentKeyLocation, "rb");
		$contents = fread($handle, filesize($parentKeyLocation));
		fclose($handle);		
		$parentKey = $contents;
		
		// Load child key						
		$handle = fopen($childKeyLocation, "rb");
		$contents = fread($handle, filesize($childKeyLocation));
		fclose($handle);		
		$childKey = base64_decode($contents);
		
		// Decrypt child key using parent key, this yields the key to perform the encryption with
		$encryptionKey = $this->pkcs5_unpad(mcrypt_decrypt($algorithm, $parentKey, $childKey, $blockMode));

		// padd the data to encrypt
		$data = $this->pkcs5_pad($data, mcrypt_get_block_size($algorithm, $blockMode));
		// encrypt the data
		$encryptedData = mcrypt_encrypt($algorithm, $encryptionKey, $data, $blockMode);
		// return the encrypted data as base64
		return base64_encode($encryptedData);
	}
	
	
	function makePayment($creditCardInfo = null, $options){
		if(empty($paymentAccountId)){
			$payment = array('clientRequestNumber' => $options['clientRequestNumber'],
									'credentials' => array(
													'username' => PAYMENT_USERNAME,
													'password' => PAYMENT_PASS,
													'merchantID' => PAYMENT_MERCHANT_IDE,
													'channelID1' => PAYMENT_MERCHANT_CHA, 
													'useTestMode' => $options['useTestMode']),
									'creditCard' => $creditCardInfo,
									'consecutiveCount' => 'ONE',							
									'merchantRequest1' => array(
														'description' => null,
														'amount' => round($options['amount'],2)						
														)
						);
		}
		else{
			$paymentInfo = $this->public_model->getPaymentInfo(array('payment_account_id' => $paymentAccountId));
			$creditCardInfo = array(
				'creditCardLast4Digits'=>$paymentInfo[0]->creditCardLast4Digits,
				'expYear' => $paymentInfo[0]->expYear,
				'expMonth' => $paymentInfo[0]->expMonth,
				'cardHolderFullName' => $paymentInfo[0]->nameOnCreditCard,						
				'cardType' => $paymentInfo[0]->creditCardType	
			);
			$payment = array('clientRequestNumber' => $options['clientRequestNumber'],
					'credentials' =>
			array(
							'username' => PAYMENT_USERNAME,
							'password' => PAYMENT_PASS,
							'merchantID' => PAYMENT_MERCHANT_IDE
			,'channelID1' => PAYMENT_MERCHANT_CHA, 'useTestMode' => $options['useTestMode']
			),
					'paymentAccount' => array(
										'paymentAccountToken' => $paymentInfo[0]->paymentToken,
										'accountReferenceNumber' => $paymentInfo[0]->paymentEngineId												
			),
					'consecutiveCount' => 'ONE',
					'merchantRequest1' => array(
										'description' => null,
										'amount' => round($options['amount'],2),								
			)
			);
		}

		//submit request
		$options = array(
				'location' => paymentGatewayPath
		);
		$soapClient = new SoapClient(paymentGatewayWSDLPath, $options);
		$action='authorize';
		if($paymentAccountId > 0){
			$action='payment';
			$response = $soapClient->submitCreditCardPaymentAccountPayment($payment);
		}else{
			$action='payment';
			$response = $soapClient->submitCreditCardPayment($payment);
		}
		$testOrder = $this->public_model->getSystemParameterValue("testOrder");
		if($response->success == true){}
	}	
	
}
?>
